home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1999 March / EnigmA AMIGA RUN 35 (1999)(G.R. Edizioni)(IT)[!][issue 1999-03].iso / earcd / -archivi / -recent1 / gencodec.lha / GenCodeC / source / WriteMainFile.c < prev    next >
C/C++ Source or Header  |  1997-02-26  |  14KB  |  444 lines

  1. #include "WriteMainFile.h"
  2. #include "Tools.h"
  3. #include "MB_protos.h"
  4. #include "MB_pragmas.h"
  5. #include "MB.h"
  6.  
  7. #include <proto/exec.h>
  8. #include <proto/dos.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11.  
  12. #define HEADER_END_MSG                 "/* GenCodeC header end */"
  13. #define    HEADER_END                    HEADER_END_MSG "\n"
  14. #define    LIBRARY_DECLARATION_MSG        "/* Declarations for libraries (inserted by GenCodeC) */"
  15. #define    LIBRARY_DECLARATION            LIBRARY_DECLARATION_MSG "\n"
  16. #define    LIBRARY_MUI_MSG                "struct Library * MUIMasterBase"
  17. #define    LIBRARY_MUI                    LIBRARY_MUI_MSG ";\n"
  18. #define    LIBRARY_LOCALE_MSG            "struct Library * LocaleBase"
  19. #define    LIBRARY_LOCALE                LIBRARY_LOCALE_MSG ";\n"
  20. #define OPEN_LOCALE_LIBRARY_MSG        "if (!(LocaleBase = OpenLibrary(\"locale.library\",38)))"
  21. #define OPEN_LOCALE_LIBRARY            "\t" OPEN_LOCALE_LIBRARY_MSG "\n"
  22. #define CLOSE_LOCALE_LIBRARY_MSG    "CloseLibrary(LocaleBase)"
  23. #define CLOSE_LOCALE_LIBRARY        CLOSE_LOCALE_LIBRARY_MSG ";\n"
  24. #define CLOSE_LIBRARY_MSG            "CloseLibrary"
  25. #define CLOSE_LIBRARY                "\t" CLOSE_LIBRARY_MSG
  26. #define    INIT_BEGIN_MSG                "/* Init() function */"
  27. #define    INIT_BEGIN                    "\n" INIT_BEGIN_MSG "\n"
  28. #define    INIT_END_MSG                "/* GenCodeC init() end */"
  29. #define    INIT_END                    "}\n" INIT_END_MSG "\n"
  30. #define    END_BEGIN_MSG                "/* End() function */"
  31. #define    END_BEGIN                    "\n" END_BEGIN_MSG "\n"
  32. #define    END_END_MSG                    "/* GenCodeC end() end */"
  33. #define    END_END                        "}\n" END_END_MSG "\n"
  34. #define    OPEN_CATALOG_MSG            "OpenAppCatalog(NULL,NULL)"
  35. #define    OPEN_CATALOG                OPEN_CATALOG_MSG ";\n"
  36. #define    CLOSE_CATALOG_MSG            "CloseAppCatalog()"
  37. #define    CLOSE_CATALOG                "\t" CLOSE_CATALOG_MSG ";\n"
  38. #define    CASE_BEGIN_MSG                "/* Insert your code between the \"case\" statement and comment \"end of case ...\" */"
  39. #define    CASE_BEGIN                    CASE_BEGIN_MSG "\n"
  40. #define    CASE_END_MSG                "/* End computing of IDCMP */"
  41. #define    CASE_END                    CASE_END_MSG "\n"
  42.  
  43. /****************************************************************************************************************/
  44. /*****                                                                                                        *****/
  45. /**                                                 WriteIDCMP                                                       **/
  46. /*****                                                                                                        *****/
  47. /****************************************************************************************************************/
  48.  
  49. static char *WriteIDCMP(FILE *file,char *buf,ULONG varnb,BOOL OldMainExists)
  50. {
  51.     int        i;
  52.     char     *name;
  53.     char    *tmp;
  54.     ULONG    type;
  55.     char     *str;
  56.     char    *str1;
  57.     char    ctmp;
  58.  
  59.     /* Write IDCMPs */
  60.     for(i=0;i<varnb;i++)
  61.     {
  62.         MB_GetVarInfo(i,
  63.                       MUIB_VarType,&type,
  64.                       MUIB_VarName,&name,
  65.                       TAG_END);
  66.         tmp = AllocMemory(strlen(name)+19,TRUE);
  67.         strcpy(tmp,"case ");
  68.         strcat(tmp,name);
  69.         strcat(tmp,":");
  70.         if (type == TYPEVAR_IDENT)
  71.         {
  72.             if (OldMainExists && (str = strstr(buf,tmp)))
  73.             {
  74.                 /* Write old IDCMP */
  75.                 strcpy(tmp,"/* end of case ");
  76.                 strcat(tmp,name);
  77.                 strcat(tmp," */");
  78.                 if (!(str1=strstr(str,tmp)))
  79.                 {
  80.                     char *msg;
  81.  
  82.                     msg = (char *)AllocMemory(60+strlen(tmp),TRUE);
  83.                     sprintf(msg,"Can't find  \"%s\"\n in MainFile generated by GenCodeC\n\n",tmp);
  84.                     DisplayMsg(msg);
  85.                     FreeMemory(msg);
  86.                     FreeMemory(tmp);
  87.  
  88.                     return NULL;
  89.                 }
  90.                 str1+=strlen(tmp);
  91.                 ctmp=*str1;
  92.                 *str1='\0';
  93.                 fprintf(file,"\t\t\t%s\n\n",str);
  94.                 *str1=ctmp;
  95.             }
  96.             else
  97.             {
  98.                 /* Write NEW IDCMP */
  99.                 fprintf(file,"\t\t\tcase %s:\n",name);
  100.                 fprintf(file,"\t\t\t/* end of case %s */\n\n",name);
  101.             }
  102.         }
  103.         FreeMemory(tmp);
  104.     }
  105.  
  106.     if (OldMainExists)
  107.         return buf;
  108.     else
  109.         return (char *)TRUE;
  110. }
  111.  
  112. /****************************************************************************************************************/
  113. /*****                                                                                                        *****/
  114. /**                                                 WriteIncludes                                                   **/
  115. /*****                                                                                                        *****/
  116. /****************************************************************************************************************/
  117.  
  118. static char *WriteIncludes(FILE *file,char *adr_file,char *HeaderFile,char *MainFile,BOOL OldMainExists)
  119. {
  120.     if (!OldMainExists)
  121.     {
  122.         fprintf(file,"\n/* Include generated by GenCodeC */\n");
  123.         fprintf(file,"#include \"%s\"\n\n",FilePart(HeaderFile));
  124.  
  125.         return NULL;
  126.     }
  127.     else
  128.     {
  129.         return CopyBlock(file,adr_file,adr_file,HEADER_END,LIBRARY_DECLARATION,
  130.                         HEADER_END_MSG,LIBRARY_DECLARATION_MSG,MainFile);
  131.     }
  132. }
  133.  
  134. /****************************************************************************************************************/
  135. /*****                                                                                                        *****/
  136. /**                                                 WriteDeclarations                                               **/
  137. /*****                                                                                                        *****/
  138. /****************************************************************************************************************/
  139.  
  140. static char *WriteDeclarations(FILE *file,char *adr_file,char *str,char *MainFile,BOOL OldMainExists,BOOL Locale)
  141. {
  142.     if (!OldMainExists)
  143.     {
  144.         fprintf(file,LIBRARY_DECLARATION);
  145.         fprintf(file,"struct Library * IntuitionBase;\n");
  146.         fprintf(file,LIBRARY_MUI);
  147.         if (Locale)
  148.                 fprintf(file,LIBRARY_LOCALE);
  149.  
  150.         return NULL;
  151.     }
  152.     else
  153.     {
  154.         fprintf(file,"%s",LIBRARY_DECLARATION);
  155.         str = CopyBlock(file,adr_file,str,LIBRARY_DECLARATION,LIBRARY_MUI,
  156.                         LIBRARY_DECLARATION_MSG,LIBRARY_MUI_MSG,MainFile);
  157.         fprintf(file,"%s",LIBRARY_MUI);
  158.         if (Locale && (!strstr(str,LIBRARY_LOCALE)))
  159.         {
  160.             fprintf(file,"%s",LIBRARY_LOCALE);
  161.             str = CopyBlock(file,adr_file,str,LIBRARY_MUI,INIT_BEGIN,
  162.                             LIBRARY_MUI_MSG,INIT_BEGIN_MSG,MainFile);
  163.         }
  164.         else
  165.         {
  166.             if (!Locale && (strstr(str,LIBRARY_LOCALE)))
  167.                 str = CopyBlock(file,adr_file,str,LIBRARY_LOCALE,INIT_BEGIN,
  168.                                 LIBRARY_LOCALE_MSG,INIT_BEGIN_MSG,MainFile);
  169.             else
  170.                 str = CopyBlock(file,adr_file,str,LIBRARY_MUI,INIT_BEGIN,
  171.                                 LIBRARY_MUI_MSG,INIT_BEGIN_MSG,MainFile);
  172.         }
  173.         return str;
  174.     }
  175. }
  176.  
  177. /****************************************************************************************************************/
  178. /*****                                                                                                        *****/
  179. /**                                             WriteInitFunction                                                   **/
  180. /*****                                                                                                        *****/
  181. /****************************************************************************************************************/
  182.  
  183. static char *WriteInitFunction(FILE *file,char *adr_file,char *str,char *MainFile,BOOL OldMainExists,BOOL Locale)
  184. {
  185.     if (!OldMainExists)
  186.     {
  187.         fprintf(file,INIT_BEGIN);
  188.         fprintf(file,"void init( void )\n");
  189.         fprintf(file,"{\n");
  190.         fprintf(file,"\tif (!(IntuitionBase = OpenLibrary(\"intuition.library\",37)))\n");
  191.         fprintf(file,"\t{\n");
  192.         fprintf(file,"\t\tprintf(\"Can't Open Intuition Library\\n\");\n");
  193.         fprintf(file,"\t\texit(20);\n");
  194.         fprintf(file,"\t}\n");
  195.         fprintf(file,"\tif (!(MUIMasterBase = OpenLibrary(MUIMASTER_NAME,MUIMASTER_VMIN)))\n");
  196.         fprintf(file,"\t{\n");
  197.         fprintf(file,"\t\tprintf(\"Can't Open MUIMaster Library\\n\");\n");
  198.         fprintf(file,"\t\tCloseLibrary(IntuitionBase);\n");
  199.         fprintf(file,"\t\texit(20);\n");
  200.         fprintf(file,"\t}\n");
  201.         if (Locale)
  202.         {
  203.             fprintf(file,"%s",OPEN_LOCALE_LIBRARY);
  204.             fprintf(file,"\t{\n");
  205.             fprintf(file,"\t\tprintf(\"Can't Open Locale Library\\n\");\n");
  206.             fprintf(file,"\t\tprintf(\"Built-in Language will be used !!!\\n\");\n");
  207.             fprintf(file,"\t}\n");
  208.             fprintf(file,"\tOpenAppCatalog(NULL,NULL);\n");
  209.         }
  210.         fprintf(file,INIT_END);
  211.  
  212.         return NULL;
  213.     }
  214.     else
  215.     {
  216.         fprintf(file,INIT_BEGIN);
  217.         if (Locale && !(strstr(str,OPEN_LOCALE_LIBRARY)))
  218.         {
  219.             str = CopyBlock(file,adr_file,str,INIT_BEGIN,INIT_END,
  220.                             INIT_BEGIN_MSG,INIT_END_MSG,MainFile);
  221.             fprintf(file,"%s",OPEN_LOCALE_LIBRARY);
  222.             fprintf(file,"\t{\n");
  223.             fprintf(file,"\t\tprintf(\"Can't Open Locale Library\\n\");\n");
  224.             fprintf(file,"\t\tprintf(\"Built-in Language will be used !!!\\n\");\n");
  225.             fprintf(file,"\t}\n");
  226.             fprintf(file,"\tOpenAppCatalog(NULL,NULL);\n");
  227.         }
  228.         else
  229.         {
  230.             if (!Locale && (strstr(str,OPEN_LOCALE_LIBRARY)))
  231.             {
  232.                 str = CopyBlock(file,adr_file,str,INIT_BEGIN,OPEN_LOCALE_LIBRARY,
  233.                                 INIT_BEGIN_MSG,OPEN_LOCALE_LIBRARY_MSG,MainFile);
  234.                 str = CopyBlock(file,adr_file,str,OPEN_CATALOG,INIT_END,
  235.                                 OPEN_CATALOG_MSG,INIT_END_MSG,MainFile);
  236.             }
  237.             else
  238.             {
  239.                 str = CopyBlock(file,adr_file,str,INIT_BEGIN,INIT_END,
  240.                                 INIT_BEGIN_MSG,INIT_END_MSG,MainFile);
  241.             }
  242.         }
  243.         fprintf(file,INIT_END);
  244.  
  245.         return str;
  246.     }
  247. }
  248.  
  249. /****************************************************************************************************************/
  250. /*****                                                                                                        *****/
  251. /**                                             WriteEndFunction                                                   **/
  252. /*****                                                                                                        *****/
  253. /****************************************************************************************************************/
  254.  
  255. static char *WriteEndFunction(FILE *file,char *adr_file,char *str,char *MainFile,BOOL OldMainExists,BOOL Locale)
  256. {
  257.     if (!OldMainExists)
  258.     {
  259.         fprintf(file,END_BEGIN);
  260.         fprintf(file,"void end( void )\n{\n");
  261.         if (Locale)
  262.         {
  263.             fprintf(file,"%s",CLOSE_CATALOG);
  264.             fprintf(file,"\tif (LocaleBase)\n");
  265.             fprintf(file,"\t\t%s",CLOSE_LOCALE_LIBRARY);
  266.         }
  267.         fprintf(file,"\tCloseLibrary(MUIMasterBase);\n");
  268.         fprintf(file,"\tCloseLibrary(IntuitionBase);\n");
  269.         fprintf(file,"\texit(20);\n");
  270.         fprintf(file,END_END);
  271.         fprintf(file,"\n");
  272.  
  273.         return NULL;
  274.     }
  275.     else
  276.     {
  277.         fprintf(file,END_BEGIN);
  278.         if (Locale && !(strstr(str,CLOSE_CATALOG)))
  279.         {
  280.             str = CopyBlock(file,adr_file,str,END_BEGIN,CLOSE_LIBRARY,
  281.                             END_BEGIN_MSG,CLOSE_LIBRARY_MSG,MainFile);
  282.             fprintf(file,"%s",CLOSE_CATALOG);
  283.             fprintf(file,"\tif (LocaleBase)\n");
  284.             fprintf(file,"\t\t%s",CLOSE_LOCALE_LIBRARY);
  285.             fprintf(file,"%s",CLOSE_LIBRARY);
  286.             str = CopyBlock(file,adr_file,str,CLOSE_LIBRARY,END_END,
  287.                             CLOSE_LIBRARY_MSG,END_END_MSG,MainFile);
  288.         }
  289.         else
  290.         {
  291.             if (!Locale && (strstr(str,CLOSE_CATALOG)))
  292.             {
  293.                 str = CopyBlock(file,adr_file,str,END_BEGIN,CLOSE_CATALOG,
  294.                                 END_BEGIN_MSG,CLOSE_CATALOG_MSG,MainFile);
  295.                 str = CopyBlock(file,adr_file,str,CLOSE_LOCALE_LIBRARY,END_END,
  296.                                 CLOSE_LOCALE_LIBRARY_MSG,END_END_MSG,MainFile);
  297.             }
  298.             else
  299.             {
  300.                 str = CopyBlock(file,adr_file,str,END_BEGIN,END_END,
  301.                                 END_BEGIN_MSG,END_END_MSG,MainFile);
  302.             }
  303.         }
  304.         fprintf(file,END_END);
  305.  
  306.         return str;
  307.     }
  308. }
  309.  
  310. /****************************************************************************************************************/
  311. /*****                                                                                                        *****/
  312. /**                                             WriteMainFunction                                                   **/
  313. /*****                                                                                                        *****/
  314. /****************************************************************************************************************/
  315.  
  316. static void WriteMainFunction(FILE *file,char *adr_file,char *str,char *MainFile,char *name,
  317.                               BOOL OldMainExists,ULONG varnb)
  318. {
  319.     if (!OldMainExists)
  320.     {
  321.         fprintf(file,"/* Main Function inserted by GenCodeC */\n");
  322.         fprintf(file,"int main(int argc,char **argv)\n");
  323.         fprintf(file,"{\n");
  324.         fprintf(file,"\tstruct Obj%s * %s = NULL;    /* Object */\n",name,name);
  325.         fprintf(file,"\tBOOL\trunning = TRUE;\n");
  326.         fprintf(file,"\tULONG\tsignal;\n\n");
  327.         fprintf(file,"\t/* Program initialisation : generated by GenCodeC */\n");
  328.         fprintf(file,"\tinit();\n\n");
  329.         fprintf(file,"\t/* Create Object : generated by GenCodeC */\n");
  330.         fprintf(file,"\tif (!(%s = Create%s()))\n",name,name);
  331.         fprintf(file,"\t{\n");
  332.         fprintf(file,"\t\tprintf(\"Can't Create %s\\n\");\n",name);
  333.         fprintf(file,"\t\tend();\n");
  334.         fprintf(file,"\t}\n\n");
  335.         fprintf(file,"\twhile (running)\n");
  336.         fprintf(file,"\t{\n");
  337.         fprintf(file,"\t\tswitch (DoMethod(%s->%s,MUIM_Application_Input,&signal))\n",name,name);
  338.         fprintf(file,"\t\t{\n");
  339.         fprintf(file,"\t\t\tcase MUIV_Application_ReturnID_Quit:\n");
  340.         fprintf(file,"\t\t\t\trunning = FALSE;\n");
  341.         fprintf(file,"\t\t\t\tbreak;\n\n");
  342.         fprintf(file,"\t\t\t%s",CASE_BEGIN);
  343.     }
  344.     else
  345.     {
  346.         str = CopyBlock(file,adr_file,str,END_END,CASE_BEGIN,
  347.                         END_END_MSG,CASE_BEGIN_MSG,MainFile);
  348.         fprintf(file,CASE_BEGIN);
  349.     }
  350.  
  351.     if (!(str = WriteIDCMP(file,str,varnb,OldMainExists)))
  352.         return;
  353.  
  354.     if (!OldMainExists)
  355.     {
  356.         fprintf(file,"\t\t\t/* End computing of IDCMP */\n\n");
  357.         fprintf(file,"\t\t\tdefault:\n");
  358.         fprintf(file,"\t\t\t\tbreak;\n");
  359.         fprintf(file,"\t\t}\n");
  360.         fprintf(file,"\t\tif (running && signal) Wait(signal);\n");
  361.         fprintf(file,"\t}\n");
  362.         fprintf(file,"\tDispose%s(%s);\n",name,name);
  363.         fprintf(file,"\tend();\n");
  364.         fprintf(file,"}\n");
  365.     }
  366.     else
  367.     {
  368.         fprintf(file,"\t\t\t%s",CASE_END);
  369.         CopyBlock(file,adr_file,str,CASE_END,NULL,
  370.                   CASE_END_MSG,NULL,MainFile);
  371.     }
  372. }
  373.  
  374. /****************************************************************************************************************/
  375. /*****                                                                                                        *****/
  376. /**                                             WriteMainFile                                                       **/
  377. /*****                                                                                                        *****/
  378. /****************************************************************************************************************/
  379.  
  380. void WriteMainFile(char *HeaderFile,char *MainFile,char *MainHeaderText,ULONG varnb,BOOL Locale)
  381. {
  382.     BPTR    BakFile;
  383.     char    *adr_file = NULL;
  384.     char    *str;
  385.     char    *buffer     = NULL;
  386.     char    *name = NULL;
  387.     BOOL    OldMainExists = FALSE;
  388.     FILE    *file;
  389.  
  390.  
  391.     /* If the file already exists, we load it in memory and we save it in a .bak file*/
  392.     if (adr_file = LoadFileInRAM(MainFile,FALSE))
  393.     {
  394.         OldMainExists = TRUE;
  395.         buffer = AllocMemory(strlen(MainFile)+4+1,TRUE);
  396.         strcpy(buffer,MainFile);
  397.         strcat(buffer,".bak");
  398.         if (BakFile = OpenFile(buffer,MODE_NEWFILE,FALSE))
  399.         {
  400.             FWrite(BakFile,adr_file,strlen(adr_file),1);
  401.             CloseFile(BakFile);
  402.         }
  403.         else
  404.         {
  405.             DisplayMsg("Can't create a backup of your Main file !!! \n");
  406.         }
  407.         FreeMemory(buffer);
  408.     }
  409.  
  410.     MB_GetVarInfo(0, MUIB_VarName, &name, TAG_END);
  411.  
  412.     if (file = fopenFile(MainFile,"w",FALSE))
  413.     {
  414.         fprintf(file,"/* Main-Header File inserted by GenCodeC */\n");
  415.  
  416.         /* Copy Main-Header into MainFile */
  417.         fprintf(file,"%s",MainHeaderText);
  418.  
  419.         fprintf(file,HEADER_END);
  420.  
  421.         str = WriteIncludes(file,adr_file,HeaderFile,MainFile,OldMainExists);
  422.  
  423.         str = WriteDeclarations(file,adr_file,str,MainFile,OldMainExists,Locale);
  424.  
  425.         str = WriteInitFunction(file,adr_file,str,MainFile,OldMainExists,Locale);
  426.  
  427.         if (OldMainExists)
  428.             str = CopyBlock(file,adr_file,str,INIT_END,END_BEGIN,
  429.                             INIT_END_MSG,END_BEGIN_MSG,MainFile);
  430.  
  431.         str = WriteEndFunction(file,adr_file,str,MainFile,OldMainExists,Locale);
  432.  
  433.         WriteMainFunction(file,adr_file,str,MainFile,name,OldMainExists,varnb);
  434.  
  435.         fcloseFile(file);
  436.         FreeMemory(adr_file);
  437.     }
  438.     else
  439.     {
  440.         DisplayMsg("Unable to create Main file !\n");
  441.         FreeMemory(adr_file);
  442.     }
  443. }
  444.